﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Projecturo.Model;
using System.Threading;
using Projecturo.ProjecturoClient.Connection;
using System.Collections.ObjectModel;

namespace Projecturo.ProjecturoClient.ViewModel.UserManagement
{
    public class UserViewModel : WorkspaceViewModel
    {
        private User user = new User();
        private ObservableCollection<Projecturo.Model.Project> projects = new ObservableCollection<Projecturo.Model.Project>();

        public Int64 Id
        {
            get { return user.Id; }
            set
            {
                if (user.Id == value)
                    return;
                user.Id = value;
                OnPropertyChanged("Id");
            }
        }

        public string Username
        {
            get { return user.Username; }
            set
            {
                if (user.Username == value)
                    return;
                user.Username = value;
                OnPropertyChanged("Username");
            }
        }

        public string FirstName
        {
            get { return user.FirstName; }
            set
            {
                if (user.FirstName == value)
                    return;
                user.FirstName = value;
                OnPropertyChanged("FirstName");
            }
        }

        public string LastName
        {
            get { return user.LastName; }
            set
            {
                if (user.LastName == value)
                    return;
                user.LastName = value;
                OnPropertyChanged("LastName");
            }
        }

        public string ShortName
        {
            get { return user.ShortName; }
            set
            {
                if (user.ShortName == value)
                    return;
                user.ShortName = value;
                OnPropertyChanged("ShortName");
            }
        }

        public string Password
        {
            get { return user.Password; }
            set
            {
                if (user.Password == value)
                    return;
                user.Password = value;
                OnPropertyChanged("Password");
            }
        }

        public UserRoles Role
        {
            get { return user.Role; }
            set
            {
                if (user.Role == value)
                    return;
                user.Role = value;
                OnPropertyChanged("Role");
            }
        }

        public bool IsActive
        {
            get { return user.IsActive; }
            set
            {
                if (user.IsActive == value)
                    return;
                user.IsActive = value;
                OnPropertyChanged("IsActive");
            }
        }

        public ObservableCollection<Projecturo.Model.Project> Projects
        {
            get { return this.projects; }
        }

        protected override void Save()
        {
            if (this.user.Projects == null)
                this.user.Projects = new List<Projecturo.Model.Project>();

            this.user.Projects.Clear();
            foreach (Projecturo.Model.Project project in this.projects)
                this.user.Projects.Add(project);

            Connector.Connectivity.Storage.Save(this.user);
        }

        protected override void Delete()
        {
            Connector.Connectivity.Storage.Delete(this.user);
        }

        public override void SetModel(object model)
        {
            this.user = model as User;
            if (this.projects == null)
                this.projects = new ObservableCollection<Projecturo.Model.Project>();
            else
                this.projects.Clear();
            foreach (Projecturo.Model.Project project in user.Projects)
            {
                this.projects.Add(project);
            }
        }
    }
}
